from flask import Blueprint,request,jsonify
import json
from db import db1
from utils.acl import acl
from config import get_data
from utils.rs import rs
from utils.jwttoken import jwta
import datetime

bp_zuoye=Blueprint('zuoye',__name__)

# 登录
@bp_zuoye.route('/denglu',methods=['POST'])
def denglu():
    data=get_data()
    username=data['username']
    print(username,111)
    print(type(username))
    sql="select * from xiao_user where username='%s'"%(str(username))
    res=db1.find(sql)
    if data['password']==res['password']:
        return jsonify({'code':200,'id':res['id']})
    return jsonify({'msg':'账号密码错误'})

@bp_zuoye.route('/getgongneng')
def getgongneng():
    id=request.args.get('id')
    id=int(id)
    sql="select gongneng_id from user_gongneng where user_id=%d"%(id)
    res=db1.find_all(sql)
    print(res,666)
    list1=[i['gongneng_id'] for i in res]
    list2=[]
    for i in list1:
        sql1="select * from xiao_gongneng where id=%d"%(int(i))
        res1=db1.find(sql1)
        list2.append(res1)
    return jsonify({"list":list2,"code":200})

# 添加小说到审核表中（1是待审核，2审核通过,3审核失败）
@bp_zuoye.route('/tianjiaxiao',methods=['POST'])
def tianjiaxiao():
    data=get_data()
    sql="insert into xiao_shenhe(userid,content,type) values(%d,'%s',1)"%(int(data['userid']),data['content'])
    db1.update(sql)
    return jsonify({"code":200})

# 获取审核内容
@bp_zuoye.route('/shenhecontent')
def shenhecontent():
    sql="select * from xiao_shenhe where type=1"
    res=db1.find_all(sql)
    return jsonify({"list":res})


# 通过审核
@bp_zuoye.route('/tongshenhe',methods=['POST'])
def tongshenhe():
    data=get_data()
    now=datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d')
    sql0="select userid from xiao_shenhe where id=%d"%(int(data['zuopin_id']))
    res0=db1.find(sql0)
    # 添加记录
    sql="insert into xiao_jilu(userid,leaveword,fen,audittime,zuopin_id) values(%d,'%s',%d,'%s',%d)"%(int(res0['userid']),data['leaveword'],5,str(now),int(data['zuopin_id']))
    db1.update(sql)
    # 修改审核状态
    sql1="update xiao_shenhe set type=%d where id=%d"%(2,int(data['zuopin_id']))
    db1.update(sql1)
    # 添加积分
    sql2="select * from xiao_userjifen where userid=%d"%(int(res0['userid']))
    res=db1.find(sql2)
    if res:
        jifen=res['jifen']
        jifen=int(jifen)+5
        sql3="update xiao_userjifen set jifen=%d where userid=%d"%(jifen,int(res0['userid']))
        db1.update(sql3)
        return jsonify({"msg":"添加成功","code":200})
    else:
        sql4="insert into xiao_userjifen(userid,jifen) values(%d,%d)"%(int(res0['userid']),5)
        db1.update(sql4)
        return jsonify({"code":666})

# 没通过审核
@bp_zuoye.route('/notongshenhe',methods=['POST'])
def notongshenhe():
    data=get_data()
    sql0 = "select userid from xiao_shenhe where id=%d" % (int(data['zuopin_id']))
    res0 = db1.find(sql0)
    now = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d')
    zuopinid=int(data['zuopin_id'])
    sql="update xiao_shenhe set type=%d where id=%d"%(3,zuopinid)
    db1.update(sql)
    sql1="insert into xiao_jilu(userid,leaveword,audittime,zuopin_id) values(%d,'%s','%s',%d)"%(int(res0['userid']),data['leaveword'],str(now),int(data['zuopin_id']))
    db1.update(sql1)
    return jsonify({"code":444})

# 添加商品
@bp_zuoye.route('/addshangpin',methods=['POST'])
def addshangpin():
    data=get_data()
    sql="insert into xiao_shangpin(name,price,num) values('%s',%f,%d)"%(data['name'],float(data['price']),int(data['num']))
    db1.update(sql)
    return jsonify({"code":200})

# 获取全部商品
@bp_zuoye.route('/getshangpin')
def getshangpin():
    sql="select * from xiao_shangpin"
    res=db1.find_all(sql)
    return jsonify({"list":res})


#获取echart需要的数据
@bp_zuoye.route('/getechartdata')
def getechartdata():
    data=get_data()
    sql="select sum(fen) as s,audittime from xiao_jilu where userid=2 group by audittime"
    res=db1.find_all(sql)
    list=[]
    list1=[str(i['audittime']) for i in res]
    list2=[float(i['s']) for i in res]
    list.append(list1)
    list.append(list2)

    print(res,666)
    return jsonify({"code":200,"list":list})




